<template>
  <div class="dailyProductionQuota">
    <DFPageList ref="dfPageListRef" :page-options="pageOptions" />
  </div>
</template>

<script lang="ts" setup name="DailyProductionQuota">
  import { ref } from 'vue';
  import lang from './lang';
  import { useMixins } from '@/hooks/useMixins';
  import { getDateStr, disablePreviousDate } from '@/utils/dateUtil';
  import { pageDailyProductionQuota } from '@/api/reportForm/dailyProductionQuota';

  const { loadLangFn } = useMixins();

  const dfPageListRef: any = ref(null);

  loadLangFn(lang);

  const reportTime = ref({
    createEndTime: `${getDateStr(-1)} 23:59:59`,
    createStartTime: `${getDateStr(-1)} 00:00:00`,
    defaultValue: [
      new Date(`${getDateStr(-1)} 00:00:00`).getTime(),
      new Date(`${getDateStr(-1)} 23:59:59`).getTime(),
    ],
  });

  const setRequestDataFn = (e) => {
    const { entity } = e;
    entity.reportTimeStartTime = new Date(reportTime.value.createStartTime).getTime();
    entity.reportTimeEndTime = new Date(reportTime.value.createEndTime).getTime();

    delete entity.date;
    // entity.exceptionFlag = 0; //异常标识 0无异常，1有异常
    return {
      ...e,
      entity,
    };
  };

  const beforeResetFn = () => {
    reportTime.value.createStartTime = `${getDateStr(-1)} 00:00:00`;
    reportTime.value.createEndTime = `${getDateStr(-1)} 23:59:59`;
  };

  const pageOptions = {
    searchForm: {
      formSchemas: [
        {
          label: 'date',
          common: true,
          component: 'NDatePicker',
          defaultValue: reportTime.value.defaultValue,
          componentProps: {
            clearable: false,
            type: 'daterange',
            valueFormat: 'yyyy-MM-dd',
            format: 'yyyy-MM-dd',
            defaultTime: ['00:00:00', '23:59:59'],
            isDateDisabled: disablePreviousDate,
            onUpdateValue: (_, v: string[]) => {
              if (v) {
                reportTime.value.createStartTime = v[0];
                reportTime.value.createEndTime = v[1];
              } else {
                reportTime.value.createStartTime = '';
                reportTime.value.createEndTime = '';
              }
            },
          },
        },
      ],
      beforeResetFn,
    },
    table: {
      hasCreate: false,
      columns: [
        {
          label: 'reportTime',
          prop: 'reportDate'
        },
        {
          label: 'machineCode',
        },
        {
          label: 'machineName',
        },
        {
          label: 'shiftName',
        },
        {
          label: 'completedOrders',
        },
        {
          label: 'completedNum',
        },
        {
          label: 'goodNum',
        },
        {
          label: 'goodArea',
        },
        {
          label: 'badNum',
        },
        {
          label: 'badRate',
        },
        {
          label: 'debugTime',
        },
        {
          label: 'productTime',
        },
        {
          label: 'shutdownTime',
        },
        {
          label: 'avgSpeed',
        },
        {
          label: 'electricityConsumption',
        },
        {
          label: 'waterConsumption',
        },
      ],
      requestFn: pageDailyProductionQuota,
      setRequestDataFn,
    },
  };
</script>
